<?php
//公共函数文件

//统一接口返回
function displayJson($code,$msg,$data = array())
{
    $ret['code']   = $code;
    $ret['msg']    = $msg;
    $ret['data']   = $data;

    echo json_encode($ret,JSON_UNESCAPED_UNICODE);die;
}

/*
* 获取用户真实IP
*/
function plum_get_realIP()
{
    $realip = '';
    if (isset($_SERVER)){
    if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
    $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
    $realip = $_SERVER["HTTP_CLIENT_IP"];
    } else {
    $realip = $_SERVER["REMOTE_ADDR"];
    }
    } else {
    if (getenv("HTTP_X_FORWARDED_FOR")){
    $realip = getenv("HTTP_X_FORWARDED_FOR");
    } else if (getenv("HTTP_CLIENT_IP")) {
    $realip = getenv("HTTP_CLIENT_IP");
    } else {
    $realip = getenv("REMOTE_ADDR");
    }
    }
    return $realip;
}

/*
 * 获取 IP  地理位置
 * @Return: array
 */
function plum_ip_get_address($ip = '')
{
    $url = 'http://api.map.baidu.com/location/ip?ip='.$ip.'&ak=2iiY6MzGuG55hRXs9GufzodP7qDKdABB&coor=bd09ll';
    if ($data=file_get_contents($url))
    {
        $data = json_decode($data,true);

        if (!empty($data) && $data['status'] == 0) {
            $result['province'] = $data['content']['address_detail']['province'];
            $result['city']     = $data['content']['address_detail']['city'];

        }else{
            $result['province'] = '未知';
            $result['city']     = '未知';
        }

        return $result;
    }
}

/*
 * 上传图片
 * @param $img //图片文件
 * @param $basedir //存储路径
 * @param string[] $imagesExt
 */
function uploadImageCommon($img,$basedir,$imagesExt=['jpg','png','jpeg','gif'])
{
    $ret['ec'] = 400;
    $ret['em'] = '文件上传失败';

    //获取上图片后缀
    $type = substr($img['name'], strrpos($img['name'], ".")+1);
    $rand = rand(1000, 9999);
    //命名图片名称
    $pics = date("YmdHis") . $rand .'.'. $type;

    if (!in_array($type,$imagesExt))
    {
        $ret['em'] = '文件格式暂不支持';
        return $ret;
    }

    //5M限制
    if ($img['size'] > 5120000)
    {
        $ret['em'] = '文件大小超过限制(5M)';
        return $ret;
    }

    //上传路径
    if (!is_dir($basedir))
    {
        //创建目录
        $mkdir = @mkdir($basedir, 0777,true);

        if (!$mkdir)
        {
            $ret['em'] = '创建目录失败';
            return $ret;
        }
    }

    $pic_path = $basedir."/{$pics}";

    //移动到指定目录，上传图片
    $res = move_uploaded_file($img['tmp_name'], $pic_path);

    if ($res)
    {
        $ret['ec']  = 200;
        $ret['em']  = '文件上传成功';
        $ret['url'] = $pic_path;
        return $ret;
    } else {
        return $ret;
    }
}

/*
 * 发送邮件相关配置
 */
function sendEmail($that)
{
    $that->load->library("PHPMailer");
    $mail = new PHPMailer\PHPMailer\PHPMailer(true);

    $mail->IsSMTP(); // 启用SMTP
    $mail->Host       = 'smtp.sina.com'; //SMTP服务器 以QQ邮箱为例子
    $mail->Port       = 465;  //邮件发送端口
    $mail->SMTPAuth   = true;  //启用SMTP认证
    $mail->SMTPSecure = "ssl";   // 设置安全验证方式为ssl

    $mail->CharSet    = "UTF-8"; //字符集
    $mail->Encoding   = "base64"; //编码方式

    $mail->Username   = 'wadye0220@sina.com';  //你的邮箱
    $mail->Password   = 'ccb234b01b8ade2d';    //密码/授权码
    $mail->Subject    = '在线聊天有新的头像投稿'; //邮件标题

    $mail->From       = 'wadye0220@sina.com';  //发件人地址（也就是你的邮箱）
    $mail->FromName   = '叶念安';  //发件人姓名

    $mail->AddAddress('1298025852@qq.com', "叶念安"); //添加收件人（地址，昵称）
    $mail->Body = '在线聊天有新的头像投稿'; //邮件主体内容

    //发送
    if ($mail->Send()) {
        return true;
    } else {
        return false;
    }
}
